package sanrenxing.dayEnd.backRepertoryTotal.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import sanrenxing.dayEnd.backRepertoryTotal.domain.SecRecAndPayStock;
import sanrenxing.dayEnd.backRepertoryTotal.domain.SecRecAndPayStockTwo;

import java.util.List;
import java.util.Map;

/**
 * 功能：证券应收应付库存的数据层接口类
 * 作者：赵思睿
 * 日期：2021/3/16 0016-14:06
 * 版本：1.0
 */
@Repository
public interface SecRecAndPayStockDao {
    /**
     * 将证券应收应付库存信息添加到数据库表中
     * @param srapStock 证券应收应付库存对象
     * @return 1：添加成功；0：添加失败
     */
    @Insert("insert into secRecAndPayStock(securId,seId,fundId,businType,Dates,sumMoney,businStat,caId,secNum)" +
            " values(seqSecRecAndPayStock.nextval,#{seId},#{fundId},#{businType},to_date('${dates}','yyyy-MM-dd'),#{sumMoney},#{businStat},#{caId},#{secNum})")
    public int add(SecRecAndPayStock srapStock);

    /**
     * 根据证券应收应付库存编号删除对象
     * @param securId 证券应收应付库存编号
     * @return 1：删除成功；0：删除失败
     */
    @Delete("delete from secRecAndPayStock where securId=#{securId}")
    public int delete(@Param("securId") String securId);

    /**
     * 分页查询所有证券应收应付库存信息
     * @param map 映射集合对象
     * @return 证券应收应付库存对象集合
     */
    @Select("<script>" +
            "select srap.securId,fs.seName as seName,f.fundName,srap.businType,srap.dates,(srap.sumMoney/srap.secNum) as sumMoney,srap.businStat,srap.caId as caId,srap.secNum as secNum from " +
            "((select rownum rn,secRecAndPayStock.* from secRecAndPayStock <where>#{end}>=rownum</where>) srap) " +
            "join fundSetup f on srap.fundId=f.fundId join financialsecurities fs on srap.seId=fs.seId  " +
            "<where>" +
            "<if test='srapStock.businType!=null'>" +
            " and businType=#{srapStock.businType}" +
            "</if>" +
            "<if test='srapStock.dates!=null'>" +
            " and to_char(dates,'yyyy-MM-dd')=#{srapStock.dates}" +
            "</if>" +
            " and rn>=#{start}" +
            "</where>" +
            "</script>")
    public List<SecRecAndPayStockTwo> selectAll(Map<String, Object> map);

    /**
     * 统计记录数
     * @return 记录数
     */
    @Select("select count(*) as counts from secRecAndPayStock")
    public int count();
}
